package com.thinkcube.edutainment.fragment;

import java.util.List;

import org.androidannotations.annotations.EFragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.SelectedItemEnlarged;
import com.thinkcube.edutainment.domain.data.CourseData;

/**
 * A fragment representing a single seachItem detail screen. This fragment is
 * either contained in a {@link seachItemListActivity} in two-pane mode (on
 * tablets) or a {@link seachItemDetailActivity} on handsets.
 */
@EFragment
public class HomeFragment extends Fragment implements OnEditorActionListener {

	String tag = HomeFragment.class.getSimpleName();
	Activity activity = this.getActivity();
	ProgressBar dialog;
	EditText search;
	public static ProgressBar progress;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View myFragmentView = inflater.inflate(
				R.layout.login_layout_activity_annotation, container, false);

		search = (EditText) myFragmentView.findViewById(R.id.searchText);
		search.setOnEditorActionListener(this);
		progress = (ProgressBar) myFragmentView
				.findViewById(R.id.progressBarMain);

		final SelectedItemEnlarged courseGallery = (SelectedItemEnlarged) myFragmentView
				.findViewById(R.id.gallery);
		DisplayMetrics metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		courseGallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {

				Bundle bundle = new Bundle();

				List<CourseData> list = courseGallery.getCourselist();

				CourseViewFragment coursview = new CourseViewFragment_();
				bundle.putParcelable("course", list.get(position));
				coursview.setArguments(bundle);
				final FragmentTransaction fragmentTransaction = getFragmentManager()
						.beginTransaction();
				fragmentTransaction.replace(R.id.myfragment, coursview,
						"newfrag");
				fragmentTransaction.addToBackStack(null);

				// Commit the transaction
				fragmentTransaction.commit();
			}
		});

		return myFragmentView;
	}

	@Override
	public boolean onEditorAction(TextView arg0, int actionId, KeyEvent arg2) {
		if (EditorInfo.IME_ACTION_SEARCH == actionId) {

			Bundle bundle = new Bundle();
			if (!search.getText().toString().isEmpty()) {
				bundle.putString("searchText", search.getText().toString());
			}
			SearchFragment searchfragment = new SearchFragment();  
			searchfragment.setArguments(bundle);
			FragmentTransaction fragmentTransaction = getFragmentManager()
					.beginTransaction();
			fragmentTransaction.replace(R.id.myfragment, searchfragment);
			fragmentTransaction.commit();

			if (getActivity().getCurrentFocus() != null
					&& getActivity().getCurrentFocus() instanceof EditText) {
				InputMethodManager imm = (InputMethodManager) getActivity()
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
			}

			return true;
		}
		return false;
	}

}
